package com.hp.handler;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.hp.entity.ExceptionJSONInfo;

/**
 * 这个和controller类似，都需要被springmvc扫描
 * @author nanlinlang
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	//@ExceptionHandler(SQLException.class)
	public String handleSQLException(HttpServletRequest request, Exception ex) {
		return "database_error";
	}

	//@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "IOException occured")
	//@ExceptionHandler(IOException.class)
	public void handleIOException() {
		// IOException handler executed
		// returning 404 error code
	}
	
	@ExceptionHandler(SQLException.class)
	public ExceptionJSONInfo handleSQLException2(HttpServletRequest request, Exception ex) {
		ExceptionJSONInfo exception = new ExceptionJSONInfo();
	    exception.setUrl(request.getRequestURL().toString());
	    exception.setMessage(ex.getMessage());
		return exception;
	}

}
